﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace WP7iPhone
{
    /// <summary>
    /// Trigger which fires when the wobble animation should start
    /// </summary>
    public class WobbleTrigger : TriggerBase<FrameworkElement>
    {
        public WobbleTrigger()
        {
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            //
            // Hook onto the global starting event
            //
            switch (Event)
            {
                case "Stop":
                    stopEvent += new EventHandler(WobbleTriggerEvent);
                    break;
                default:
                    startEvent += new EventHandler(WobbleTriggerEvent);
                    break;
            }
        }

        void WobbleTriggerEvent()
        {
            InvokeActions(null);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
        }

        /// <summary>
        /// Static method to fire the Start event for all trigger instances
        /// </summary>
        static public void Start()
        {
            startEvent();
        }

        /// <summary>
        /// Static method to fire the Stop event for all trigger instances
        /// </summary>
        static public void Stop()
        {
            stopEvent();
        }


        /// <summary>
        /// Dependency property for which event to trigger on. Should be set to either
        /// "Start" or "Stop"
        /// </summary>
        public string Event
        {
            get { return (string)GetValue(EventProperty); }
            set { SetValue(EventProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Event.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EventProperty =
            DependencyProperty.Register("Event", typeof(string), typeof(WobbleTrigger), null);

      

        //
        // The private event handlers that all instances of this trigger hook onto for notification
        //
        private delegate void EventHandler();
        static private event EventHandler startEvent;
        static private event EventHandler stopEvent;

    }
}
